#include "srbusinesspage.h"

#include <QPushButton>
#include <QLabel>
#include <QStackedWidget>
#include <QSignalMapper>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QTimer>
#include <QMessageBox>

#include <srdatabase.h>

#include "srdefine.h"

#include "subpage/SRBusinessPage"
QString g_userName;

SRBusinessPage::SRBusinessPage(QWidget *parent) : QWidget(parent)
{
    setMinimumHeight(720);
    setMaximumSize(99999,99999);
    setWindowTitle(tr("币宝(企业版)"));
    QVBoxLayout* layout_btn = new QVBoxLayout();
    QVBoxLayout* layout_right = new QVBoxLayout();
    QHBoxLayout* layout_title = new QHBoxLayout();
    QHBoxLayout* layout = new QHBoxLayout();
    pageAccount_ = new SRAccountManageWidget();
    // 子页
    stackedPage_ = new QStackedWidget();
    pageOverview_ = new SROverviewWidget();
    pageReview_ = new SRReviewAuditWidget();
    pageBusiness_ = new SRWithdrawCashAuditWidget();
    stackedPage_->addWidget(pageOverview_);
    stackedPage_->addWidget(new OEWalletManageWidget());
    stackedPage_->addWidget(pageBusiness_);
    stackedPage_->addWidget(pageReview_);
    stackedPage_->addWidget(pageAccount_);
    // 右侧呼出子页的按钮
    QWidget* wdgt_title = new QWidget();
    QPushButton* btn_close = new QPushButton("Χ");
    QPushButton* btn_minimize = new QPushButton("-");
    btn_close->setObjectName("CloseButton");
    btn_minimize->setObjectName("MinimizeButton");
    labUserName_ = new QLabel(" 当前用户姓名:  ");
    labUserName_->setObjectName("<a href='http://www.eshanren.com/'>当前用户姓名: %1</a>");


    wdgt_title->setFixedHeight(40);
    btn_close->setFixedSize(35,35);
    btn_minimize->setFixedSize(35,35);

    layout_title->addStretch(600);
    layout_title->addWidget(labUserName_);
    layout_title->addSpacing(50);
    layout_title->addWidget(btn_minimize,0,Qt::AlignTop | Qt::AlignRight);
    layout_title->addWidget(btn_close,0,Qt::AlignTop | Qt::AlignRight);
    layout_title->setMargin(0);

    wdgt_title->setLayout(layout_title);
    {
        connect(btn_close, &QPushButton::clicked,
                this, &SRBusinessPage::sigCloseClick);
        connect(btn_minimize, &QPushButton::clicked,
                this, &SRBusinessPage::sigMinimizeClick);
    }


    // 左侧导航栏
    QWidget* navigation = new QWidget();
    navigation->setStyleSheet(QString("background-color:rgb(50,50,50);margin:0px;padding:0px;"));
    QLabel* lab_logo = new QLabel("");
    lab_logo->setPixmap(QPixmap(QString(":/lab/logo_jpg")));
    lab_logo->resize(70,70);
    layout_btn->addWidget(lab_logo);
    QPushButton* btn_overview = new QPushButton(tr(""));
    btn_overview->setObjectName("sy");
    QPushButton* btn_wallet_manage = new QPushButton(tr(""));
    btn_wallet_manage->setObjectName("qbgl");
    QPushButton* btn_withdraw_cash_audit = new QPushButton(tr(""));
    btn_withdraw_cash_audit->setObjectName("tbfs");
    QPushButton* btn_account_manage = new QPushButton(tr(""));
    btn_account_manage->setObjectName("zhgl");
    QPushButton* btn_review_audit = new QPushButton(tr(""));
    btn_review_audit->setObjectName("lshg");
    QPushButton* btn_logout = new QPushButton(tr(""));
    btn_logout->setObjectName("tcdl");

    btn_overview->setStyleSheet("QPushButton{image: url(:/business/navigation/sy_press);}");
    btn_wallet_manage->setStyleSheet("QPushButton{image: url(:/business/navigation/qbgl);}");
    btn_withdraw_cash_audit->setStyleSheet("QPushButton{image: url(:/business/navigation/tbfs);}");
    btn_account_manage->setStyleSheet("QPushButton{image: url(:/business/navigation/zhgl);}");
    btn_review_audit->setStyleSheet("QPushButton{image: url(:/business/navigation/lshg);}");
    btn_logout->setStyleSheet("QPushButton{image: url(:/business/navigation/tcdl);}");



    listPage_.push_back(btn_overview);
    listPage_.push_back(btn_wallet_manage);
    listPage_.push_back(btn_withdraw_cash_audit);
    listPage_.push_back(btn_review_audit);
    listPage_.push_back(btn_account_manage);
    listPage_.push_back(btn_logout);
    {
        connect(btn_logout, &QPushButton::clicked,
                this,&SRBusinessPage::onLogout);
    }



    // 按钮与堆放页链接
    QSignalMapper *signalMapper = new QSignalMapper();
    for (int i = 0; i < listPage_.size(); ++i) {
        connect(listPage_.at(i),SIGNAL(clicked()),
                signalMapper, SLOT(map()));
        listPage_.at(i)->setFixedSize(70,70);
        signalMapper->setMapping(listPage_.at(i),listPage_.at(i));
        signalMapper->setMapping(listPage_.at(i),i);
        if (listPage_.back() == listPage_.at(i)) {
            layout_btn->addStretch(600);
        }
        layout_btn->addWidget(listPage_.at(i));
    }
    connect(signalMapper,SIGNAL(mapped(QWidget*)),
            this,SLOT(onChanggeBtnStyle(QWidget*)));
    connect(signalMapper,SIGNAL(mapped(int)),
            stackedPage_,SLOT(setCurrentIndex(int)));

    // 布局
    navigation->setLayout(layout_btn);
    layout_right->addWidget(wdgt_title);
    layout_right->addWidget(stackedPage_);
    layout->addWidget(navigation);
    layout->addLayout(layout_right);


    layout->setMargin(0);
    layout_btn->setMargin(0);


    setLayout(layout);

    // link
    connect(labUserName_, &QLabel::linkActivated,
            this, [=]{
        emit btn_account_manage->clicked();
    });

}

SRBusinessPage::~SRBusinessPage()
{
#ifdef QT_DEBUG
    qDebug() << "业务窗口被析构";
#endif
}

void SRBusinessPage::onUserPswdLogin(const QString &_user, const QString &_name)
{
    pageAccount_->setUser(_user, _name);
    if (!SRDatabase::isExist()) {
        SRDatabase::registerDB();
    }
    SRDatabase::DB()->open(_name);
    SRDatabase::DB()->writeOperateLog("登录系统", 0);
    pageOverview_->onUpdate();
    g_userName = _name;
    user_ = _user;
    name_ = _name;
    labUserName_->setText(labUserName_->objectName().arg(name_));
}

void SRBusinessPage::onLogout()
{
    if (this->pageBusiness_->isBusy()) {

        int ret = QMessageBox::question(this, "是否停止", "是否停止当前正在进行的业务操作",
                                        QMessageBox::Yes|QMessageBox::No);
        if (ret == QMessageBox::Yes){
            this->pageBusiness_->onStopImportExcel();
            this->pageBusiness_->onStopSign();
        }

        return;
    }
    SRDatabase::DB()->writeOperateLog("退出系统", 0);
//    SRDatabase::DB()->close();
    emit this->sigLogoutClick();
}

void SRBusinessPage::onChanggeBtnStyle(QWidget *_btn) {
    QPushButton* nvg_btn = qobject_cast<QPushButton*>(_btn);
    if (nvg_btn == Q_NULLPTR)
        return;
    for (auto item : listPage_) {
        const QString& temp_style = QString("QPushButton{image: url(:/business/navigation/%1)}").arg(item->objectName());
        item->setStyleSheet(temp_style);
    }

    if (pageAccount_ == stackedPage_->currentWidget()) {
        labUserName_->hide();
    }
    else {
        labUserName_->show();
    }
    const QString& style = QString("QPushButton{image: url(:/business/navigation/%1_press);}").arg(nvg_btn->objectName());
    nvg_btn->setStyleSheet(style);
}



